package bankowosc.polaczenie;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import bankowosc.dane.Harmonogram_splat;

public class Harmonogram_splatBD {

	public ArrayList<Harmonogram_splat> pobierzHarmonogr(polacz p, int nr_kredytu) {
		ArrayList<Harmonogram_splat> harmonogram = new ArrayList<>();
		Statement stmt = p.getStmt();
		String komenda = "select * from harmonogram_splat where id_kredytu="+nr_kredytu;
		try {
			ResultSet rs = stmt.executeQuery(komenda);
			while(rs.next()) {
				harmonogram.add(new Harmonogram_splat(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),
						rs.getString(5),rs.getFloat(6),rs.getFloat(7),rs.getFloat(8),rs.getFloat(9),
						rs.getFloat(10),rs.getInt(11),rs.getInt(12)));
			}
			rs.close();
			return harmonogram;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void dodajHarmStaly(polacz p, int rata, float kwota, int raty) {
		Connection c = p.getC();
		try {
			CallableStatement ctmt = c.prepareCall("{call dodaj_harmonogram_staly(?,?,?)}");
			ctmt.setInt(1, rata);
			ctmt.setFloat(2, kwota);
			ctmt.setInt(3, raty);
			ctmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void dodajHarmZmienny(polacz p, int rata, float kwota, int raty) {
		Connection c = p.getC();
		try {
			CallableStatement ctmt = c.prepareCall("{call dodaj_harmonogram_zmienny(?,?,?)}");
			ctmt.setInt(1, rata);
			ctmt.setFloat(2, kwota);
			ctmt.setInt(3, raty);
			ctmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public boolean edytujHarm(polacz p, int id_harm) {
		Connection c = p.getC();
		try {
			CallableStatement ctmt = c.prepareCall("{call edytuj_splate(?)}");
			ctmt.setInt(1, id_harm);
			ctmt.execute();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
